package au.com.handsunited.model.validators.entity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

import au.com.handsunited.model.Organisation;
import au.com.handsunited.model.Organisation;
import au.com.handsunited.model.validators.address.OrganisationLocationValidator;
import au.com.handsunited.model.validators.availability.AvailabilityValidator;
import au.com.handsunited.model.validators.contactinfo.OrganisationContactInfoValidator;

/**
 * @author arunda
 *
 */
public class OrganisationValidator extends BaseEntityValidator {


	private static Logger logger = LoggerFactory.getLogger(OrganisationValidator.class);

	public OrganisationValidator (OrganisationLocationValidator locationValidator, OrganisationContactInfoValidator contactInfoValidator, AvailabilityValidator availabilityValidator) {

		super(locationValidator, contactInfoValidator, availabilityValidator);

	}

	/**
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@Override
	public boolean supports(Class clazz) {
		return clazz.isAssignableFrom(Organisation.class);
	}

	/**
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object target, Errors errors) {

		Organisation organisation = (Organisation) target;

		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.required", "Organisation name is required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "yearFounded", "yearFounded.required", "Year founded is required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "summary", "missionStatement.required", "Mission statement is required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "description.required", "Description is required");

		if ((organisation.getServiceCategories() == null) || (organisation.getServiceCategories().size() == 0)) {
			errors.rejectValue("serviceCategories", "organisation.serviceCategories.required", "At least one service category is required");
		}

		super.validate(target, errors);
	}

}
